//
//  AddFoodViewController.m
//  ZooNgonDoiOS
//
//  Created by Trung sound on 9/17/12.
//  Copyright (c) 2012 Personal. All rights reserved.
//

#import "AddFoodViewController.h"
#import "ZooUtil.h"
#import "PickImageSourceAlertView.h"

@implementation AddFoodViewController
@synthesize addFoodDelegate;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (id)initAddFood {
    self = [super initWithNibName:nil bundle:nil];
    if (self) {
        [self initHeaderView];
        [self initContentView];
        
    }
    
    return self;
}

- (void)initHeaderView {
    _vHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 48)];
    _vHeader.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:IMG_NAV_BACKGROUND]];
    
    _btnBack = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 48, 48)];
    [_btnBack setBackgroundImage:[UIImage imageNamed:IMG_BACK_NORMAL] forState:UIControlStateNormal];
    [_btnBack setBackgroundImage:[UIImage imageNamed:IMG_BACK_SELECTED] forState:UIControlStateHighlighted];
    [_btnBack addTarget:self action:@selector(backClicked) forControlEvents:UIControlEventTouchUpInside];
    
    _lbTitle = [[UILabel alloc]initWithFrame:CGRectMake((self.view.bounds.size.width -150)/2, 10, 150, 25)];
    [_lbTitle setText:@"THÊM MÓN ĂN"];
    [_lbTitle setFont:[UIFont systemFontOfSize:18]];
    [_lbTitle setBackgroundColor:[UIColor clearColor]];
    [_lbTitle setTextColor:[UIColor whiteColor]];
        
    [_vHeader addSubview:_btnBack];
    [_vHeader addSubview:_lbTitle];
    
    [self.view addSubview:_vHeader];
    
    [_btnBack release];
    [_vHeader release];
    
}

- (void)initContentView {
    UIView *vContent = [[UIView alloc]initWithFrame:CGRectMake(10, 60, 297, 145)];
    
    _btnImageFood = [[UIButton alloc]initWithFrame:CGRectMake(0, 5, 128, 128)];
    [_btnImageFood setImage:[UIImage imageNamed:IMG_BTN_ADDFOOD_IMAGE] forState:UIControlStateNormal];
    
    _vImageFood = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 128, 128)];
    [_btnImageFood addSubview:_vImageFood];
    [_btnImageFood addTarget:self action:@selector(btnImageFoodClicked) forControlEvents:UIControlEventTouchUpInside];
    
    _imagePicker = [[UIImagePickerController alloc]init];
    _imagePicker.allowsEditing = YES;
    _imagePicker.delegate = self;
    
    _lbFoodName = [[UILabel alloc]initWithFrame:CGRectMake(133, 5, 110, 25)];
    [_lbFoodName setBackgroundColor:[UIColor clearColor]];
    [_lbFoodName setTextColor:[ZooUtil getColorFromHexa:@"ac8e4e" alpha:1.0]];
    [_lbFoodName setFont:[UIFont boldSystemFontOfSize:14]];
    [_lbFoodName setText:STRING_NAME_OF_FOOD];
    _tfFoodName = [[UITextField alloc]initWithFrame:CGRectMake(133, 33, 160, 20)];
    _tfFoodName.delegate = self;
    [_tfFoodName setPlaceholder:STRING_INPUT_NAMEFOOD];
    [_tfFoodName setTextColor:[ZooUtil getColorFromHexa:@"297e8a" alpha:1.0]];
    [_tfFoodName setFont:[UIFont systemFontOfSize:12]];
    _tfFoodName.autocapitalizationType = NO;

    UIImageView *vImgLine1 = [[UIImageView alloc]initWithFrame:CGRectMake(133, 55, 158, 2)];
    [vImgLine1 setImage:[UIImage imageNamed:IMG_ADDFOOD_LINE]];
    [vContent addSubview:vImgLine1];
    
    _lbFoodCost = [[UILabel alloc]initWithFrame:CGRectMake(133, 75, 110, 20)];
    [_lbFoodCost setBackgroundColor:[UIColor clearColor]];
    [_lbFoodCost setTextColor:[ZooUtil getColorFromHexa:@"ac8e4e" alpha:1.0]];
    [_lbFoodCost setText:STRING_COST_OF_FOOD];
    [_lbFoodCost setFont:[UIFont boldSystemFontOfSize:14]];
    _tfFoodCost = [[UITextField alloc]initWithFrame:CGRectMake(133, 105, 160, 20)];
    [_tfFoodCost setPlaceholder:STRING_INPUT_NAMECOST];
    [_tfFoodCost setFont:[UIFont systemFontOfSize:13]];
    [_tfFoodCost setTextColor:[ZooUtil getColorFromHexa:@"297e8a" alpha:1.0]];
    [_tfFoodCost setKeyboardType:UIKeyboardTypeDecimalPad];
    _tfFoodCost.delegate = self;
    _tfFoodCost.autocapitalizationType = NO;
    
    UIImageView *vImgLine2 = [[UIImageView alloc]initWithFrame:CGRectMake(133,128, 158, 2)];
    [vImgLine2 setImage:[UIImage imageNamed:IMG_ADDFOOD_LINE]];
    [vContent addSubview:vImgLine2];
    
    [vContent addSubview:_btnImageFood];
    [vContent addSubview:_lbFoodName];
    [vContent addSubview:_tfFoodName];
    [vContent addSubview:_lbFoodCost];
    [vContent addSubview:_tfFoodCost];
    
    _btnAddDone = [[UIButton alloc]initWithFrame:CGRectMake(10, 215, 297, 35)];
    [_btnAddDone addTarget:self action:@selector(btnAddDoneClicked) forControlEvents:UIControlEventTouchUpInside];
    UILabel *lbAddDone = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 297, 35)];
    [lbAddDone setText:BTN_DONE_TITLE];
    [lbAddDone setBackgroundColor:[UIColor clearColor]];
    [lbAddDone setTextAlignment:UITextAlignmentCenter];
    [lbAddDone setFont:[UIFont systemFontOfSize:13]];
    [lbAddDone setTextColor:[UIColor whiteColor]];
    [_btnAddDone addSubview:lbAddDone];
    [lbAddDone release];
    
//    [_btnAddDone setBackgroundImage:[UIImage imageNamed:IMG_BTN_ADDDONE_NORMAL] forState:UIControlEventTouchUpInside];
    [_btnAddDone setBackgroundImage:[UIImage imageNamed:IMG_BTN_ADDDONE_CLICKED] forState:UIControlStateNormal];

    _btnAddMore = [[UIButton alloc]initWithFrame:CGRectMake(10, 260, 297, 35)];
    [_btnAddMore addTarget:self action:@selector(btnAddMoreClicked) forControlEvents:UIControlEventTouchUpInside];
    UILabel *lbAddMore = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 297, 35)];
    [lbAddMore setText:STRING_TITLE_ADDMORE];
    [lbAddMore setBackgroundColor:[UIColor clearColor]];
    [lbAddMore setTextAlignment:UITextAlignmentCenter];
    [lbAddMore setFont:[UIFont systemFontOfSize:13]];
    [lbAddMore setTextColor:[UIColor whiteColor]];
    [_btnAddMore addSubview:lbAddMore];
    [lbAddMore release];
    
//    [_btnAddMore setBackgroundImage:[UIImage imageNamed:IMG_BTN_ADDMORE_NORMAL] forState:UIControlEventTouchUpInside];
    [_btnAddMore setBackgroundImage:[UIImage imageNamed:IMG_BTN_ADDMORE_CLICKED] forState:UIControlStateNormal];
    
    [self.view addSubview:vContent];
    [self.view addSubview:_btnAddDone];
    [self.view addSubview:_btnAddMore];
    
    [_btnImageFood release];
    [_btnAddDone release];
    [_btnAddMore release];
    [_vImageFood release];
    [vImgLine1 release];
    [vImgLine2 release];
}

- (void)backClicked {
    [self dismissModalViewControllerAnimated:NO];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [_tfFoodName resignFirstResponder];
    [_tfFoodCost resignFirstResponder];
}

#pragma mark - TextField Delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [_tfFoodName resignFirstResponder];
    [_tfFoodCost resignFirstResponder];
}

#pragma mark - Control button clicked
- (void)btnImageFoodClicked {
    PickImageSourceAlertView *pickImage = [[PickImageSourceAlertView alloc]initWithTitle:nil message:@"" delegate:nil cancelButtonTitle:@"" otherButtonTitles:nil];

    pickImage.pickSourceDelegate = self;
    [pickImage show];
    [pickImage release];
}

- (void)btnAddDoneClicked {
    ZooFoodBean *zooFoodbean = [[ZooFoodBean alloc]init];
    zooFoodbean.foodName = _tfFoodName.text;
    zooFoodbean.foodCost = _tfFoodCost.text;

    if (addFoodDelegate != nil) {
        [addFoodDelegate addFoodDone:zooFoodbean state:YES];
    }
    [self dismissModalViewControllerAnimated:YES];
}

- (void)btnAddMoreClicked {
    [_vImageFood setImage:nil];
}

#pragma mark - PickImageSourceDelegate
- (void)pickCameraSource {
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        [_imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
        [_imagePicker setDelegate:self];
        [self presentModalViewController:_imagePicker animated:YES];
    } else {
        [ZooUtil showSimpleAlert:@"Camera Unavaible"];
    }
}


- (void)pickGallerySource {
    [_imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];

    [self presentModalViewController:_imagePicker animated:YES];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    NSLog(@"%@",[info objectForKey:UIImagePickerControllerEditedImage]);
    [_vImageFood setImage:[info objectForKey:UIImagePickerControllerEditedImage]];
        //    [_vImageFood setImage:image];
    [self dismissModalViewControllerAnimated:YES];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
    NSLog(@"%@",[editingInfo objectForKey:UIImagePickerControllerEditedImage]);
    [_vImageFood setImage:[editingInfo objectForKey:UIImagePickerControllerEditedImage]];
//    [_vImageFood setImage:image];
    [self dismissModalViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
}
*/

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
